package com.java.shop.product.controller;

import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.shop.product.entity.ProductAttrValueEntity;
import com.java.shop.product.service.AttrService;
import com.java.shop.product.service.ProductAttrValueService;
import com.java.shop.product.vo.AttrRespVo;
import com.java.shop.product.vo.AttrVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 *属性说明：例【手机商品】
 *     基本属性：【规格与包装】产品名称、上市月份等，基本属性对应分组【规格与包装】->主体[属性分组]->产品名称[基本属性]
 *     销售属性：黑色、128G等
 *
 * @author cxq
 * @email 137512476@qq.com
 * @date 2021-03-26 11:19:51
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    //@RequiresPermissions("product:attr:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = attrService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }

    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{id}")
    //@RequiresPermissions("product:attr:list")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable("id") Long id,
                      @PathVariable("type") String type){
        PageUtils page = attrService.queryPage(params,id,type);
        return R.ok().put("page", page);
    }

    @ApiOperation("获取spu规格")
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> data = productAttrValueService.baseAttrForSpu(spuId);
        return R.ok().put(data);
    }

    //TODO 更新操作未写 https://easydoc.xyz/s/78237135/ZUqEdvA4/GhnJ0L85,老师的做法是删除所有包含spuid的值，然后添加
    @PostMapping("/update/{spuId}")
    public R updateEntites(@PathVariable("spuId") Long spuId,
                    @RequestBody List<ProductAttrValueEntity> entites){
        return R.ok().put("AttrController:updateEntites操作未写");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
//		attrService.save(attr);
        attrService.saveAndOther(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateDetails(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
